/**
 * <p>
 * Title: AppUtils.java
 * </p>
 * <p>
 * Description: 应用程序信息、管理工具类
 * </p>
 * <p>
 * 1、用于获取应用程序相关信息；
 * 2、应用管理应用程序中的activity；
 * 3、判断应用程序中的服务是否启动
 * </p>
 *
 * @Copyright: Copyright (c) 2016
 * @author zhangqy
 * @date 2016年8月17日 下午4:38:36
 * @version V1.0
 */
package com.crb.smartcard.utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

import java.security.MessageDigest;

/**
 * <p>
 * Title: AppUtils
 */
public class AppUtils {
    private static final String TAG = "AppUtils";

    /**
     * Title: getVersionCode
     * Description: 获取应用程序版本号
     */
    public static int getVersionCode(Context context) {
        int versionCode = -1;
        if (context == null) {
            return versionCode;
        }
        try {
            @SuppressLint("WrongConstant")
            PackageInfo packageInfo = context.getPackageManager()
                    .getPackageInfo(getPackageName(context), 1);
            if (packageInfo != null) {
                versionCode = packageInfo.versionCode;
            }
        } catch (Exception ignored) {
        }
        return versionCode;
    }

    public static String getPackageName(Context context){
        if (context == null) {
            return null;
        }
        return context.getPackageName();
    }

    /**
     * 通过Context获取当前的 MD5值
     *
     * @param context
     */
    public static String getAppMd5(Context context) {

        PackageManager pm = context.getPackageManager();
        PackageInfo packageInfo = null;
        try {
            packageInfo = pm.getPackageInfo(context.getPackageName(), PackageManager.GET_SIGNATURES);
        } catch (PackageManager.NameNotFoundException ignored) {
        }
        String md5Fingerprint = null;
        if (packageInfo != null) {
            ApplicationInfo applicationInfo = packageInfo.applicationInfo;
            boolean debug = (applicationInfo.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
            try {
                md5Fingerprint = doFingerprint(packageInfo.signatures[0].toByteArray(), "MD5");
            } catch (Exception ignored) {
            }
        }
        return md5Fingerprint;
    }

    /**
     * 通过Context获取当前的 SHA1值
     *
     * @param context
     */
    //debug 7c:0d:95:c3:12:9a:30:33:68:8a:bc:0e:07:29:1d:08:a5:71:78:60
    //release 3f:7f:d8:7d:2b:bb:cc:c2:64:2c:16:c1:3e:93:35:cc:08:9b:18:51
    public static String getAppSha1(Context context) {

        PackageManager pm = context.getPackageManager();
        PackageInfo packageInfo = null;
        try {
            packageInfo = pm.getPackageInfo(context.getPackageName(), PackageManager.GET_SIGNATURES);
        } catch (PackageManager.NameNotFoundException ignored) {
        }
        String md5Fingerprint = null;
        if (packageInfo != null) {
            ApplicationInfo applicationInfo = packageInfo.applicationInfo;
            boolean debug = (applicationInfo.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
            try {
                md5Fingerprint = doFingerprint(packageInfo.signatures[0].toByteArray(), "SHA1");
            } catch (Exception ignored) {
            }
        }
        return md5Fingerprint.replaceAll(":","");
    }


    /**
     * @param certificateBytes 获取到应用的signature值
     * @param algorithm        在上文指定MD5算法
     * @return md5签名
     */
    private static String doFingerprint(byte[] certificateBytes, String algorithm) throws Exception {
        MessageDigest md = MessageDigest.getInstance(algorithm);
        md.update(certificateBytes);
        byte[] digest = md.digest();

        String toRet = "";
        for (int i = 0; i < digest.length; i++) {
            if (i != 0) {
                toRet += ":";
            }
            int b = digest[i] & 0xff;
            String hex = Integer.toHexString(b);
            if (hex.length() == 1) {
                toRet += "0";
            }
            toRet += hex;
        }
        return toRet;
    }
}
